'use strict'

//cookie，session 客户端的存储方式
const titbit = require('titbit')
const {cookie, session} = require('titbit-toolkit')

const app = new titbit({
    debug: true
})

/* 用use添加的按顺序执行 */
//localhost:1921?key=12345  ‘key=12345’查询字符串
/* 
    c ==> routepath/content/:id
          path/content/1234 
*/
app.use(async (c,next)=>{ 
    if(c.query.key !== '12345'){  
        return c.status(403).send('deny')
    }
    await next() //下一个中间件函数，如果不写不会执行
}, {group:session})

app.use(async (c,next)=>{
    console.log('time start')
    let startTime = Date.now()
    await next()
    let endTime = Date.now()
    console.log(endTime - startTime, 'ms')
})

//启用扩展，先启用cookie，后启用session，两者不是必须要同时出现
app.use(new cookie).use(new session)

app.use(async (c,next)=>{
    console.log('home start')
    await next()
    console.log('home end')
}, {group:'home',method:['GET']})

app.get('/',async c=>{
    c.send('ok')
},{name:'主页',group:'home'})

app.get('/sess/:x/:y',async c=>{
    c.setSession('x', c.param.x)
    c.setSession('y', c.param.y)

    c.send(c.getSession())

}, {group:'session'})

app.run(1921)